/**navigator
 * <script src = 'http://172.20.10.6:8088/r.js' async></script>
 */

var BPparams = {};
var _appid = '';
var _navigator = {};
var _webInfo = {};
var BPAPIurl= 'http://127.0.0.1:8088/'
/**
 * 
 * @param {*} params type:操作类型（项目可自定各种类型） name:操作名称  content:操作详细内容
 */
function BPrecord(params = {}){
	var Rparams = Object.assign(BPparams, params)
	console.log(Rparams)
	console.log('BPrecord')
	//拼接参数串
	var args = ''
	for (var i in Rparams) {
		if (args != '') {
			args += '&'
		}
		args += i + '=' + encodeURIComponent(Rparams[i])
	}
	//通过Image对象请求后端脚本
	var img = new Image(1, 1)
	img.src = BPAPIurl+'record?' + args
}

(function () {
	// 获取APPID
	BPparams.appid = ''
	let jsTagNameArr = document.getElementsByTagName('script')
	for(let i=0; i < jsTagNameArr.length; i++){
		let src = jsTagNameArr[i].src
		if(src.indexOf('r.js?appid=') !== -1){
			let arr = src.split('?')[1].split('&')
			for(let j=0; j < arr.length; j++){
				if(arr[j].indexOf('appid=')!==-1){
					let appArr = arr[j].split('=')
					BPparams.appid = appArr[1]
					_appid = appArr[1]
					console.log(_appid)
				}
			}
		}
	}
	//Document对象数据
	if (document) {
		_webInfo.domain = document.domain || ''
		_webInfo.url = document.URL || ''
		_webInfo.title = document.title || ''
		_webInfo.referrer = document.referrer || ''
		_webInfo.cookie = document.cookie || ''
	}
	//navigator对象数据
	if (navigator) {
		BPparams.language = navigator.language || ''
		BPparams.userAgent = navigator.userAgent || ''
		_navigator.language = navigator.language || ''
		_navigator.userAgent = navigator.userAgent || ''
	}
	// console.log(BPparams)
	/**
	 * 访问记录
	 */
	//拼接参数串
	var args = ''
	for (var i in BPparams) {
		if (args != '') {
			args += '&'
		}
		args += i + '=' + encodeURIComponent(BPparams[i])
	}
	//通过Image对象请求后端脚本
	var img = new Image();
	var rnd_id = "_img_" + Math.random();

	window[rnd_id] = img; // 全局变量引用

	img.onload = img.onerror = function () {
		window[rnd_id] = null; // 删除全局变量引用
	}

	img.src = BPAPIurl+'stay?' + args

	/**
 	* 访问时长
	*/
	var BPsecond = 0
	var BPInt = setInterval(()=>{
		BPsecond++
	},1000)
	window.addEventListener('unload', function(event) {
		// if (event.oldState == 'passive' && event.newState == 'hidden') {
		// 	navigator.sendBeacon('/log', postData);
		// }
		// if(event.oldState == 'passive' && event.newState == 'hidden'){
			clearInterval(BPInt)
			console.log('visibilitychange=======')
			var Rparams = Object.assign(BPparams, {stayTime: BPsecond})
			//拼接参数串
			var args = ''
			for (var i in Rparams) {
				if (args != '') {
					args += '&'
				}
				args += i + '=' + encodeURIComponent(Rparams[i])
			}

			
			//通过Image对象请求后端脚本
			var img = new Image();
			var rnd_id = "_img_" + Math.random();

			window[rnd_id] = img; // 全局变量引用

			img.onload = img.onerror = function () {
				window[rnd_id] = null; // 删除全局变量引用
			}
			var date = new Date();
  			var logTime = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+ ' ' + date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()
			img.src = BPAPIurl+'stay?' + args + '&==========logTime='+logTime

			// let result = navigator.sendBeacon(BPAPIurl+'stay?' + args + '&==========logTime='+logTime, {});
			BPsecond = 0
		// }
		// }else {
			// BPsecond = 0
			// BPInt = setInterval(()=>{
			// 	BPsecond++
			// },1000)
		// }
		
	})

	/**
	 * 点击事件
	 */
	window.addEventListener('click', function(event) {
		if(encodeURIComponent(event.target.outerHTML).length<1000){

		
			console.log(event)
			//通过Image对象请求后端脚本
			var img = new Image();
			var rnd_id = "_img_" + Math.random();

			window[rnd_id] = img; // 全局变量引用

			img.onload = img.onerror = function () {
				window[rnd_id] = null; // 删除全局变量引用
			}
			console.log(encodeURIComponent(JSON.stringify(_navigator)))

			// img.src = BPAPIurl+'click/add?appid='+_appid+'&navigator=' + encodeURIComponent(JSON.stringify(_navigator)) +'&webInfo='+ encodeURIComponent(JSON.stringify(_webInfo)) + '&htmlInfo=' + event.target.innerHTML
			img.src = BPAPIurl+'click/add?url='+encodeURIComponent(this.location.href)+'&userId=' + sessionStorage.userId +'&user_agent='+ navigator.userAgent + '&operation_content=' + encodeURIComponent(event.target.outerHTML)
		}
	}, true)

})();